package com.rfid.afd.presenter;

import android.os.Handler;

import com.rfid.afd.activity.view.IStatisticsView;
import com.rfid.afd.model.StatisticsModel;
import com.rfid.db.bean.Operation;
import com.rfid.db.bean.Rfid;
import com.rfid.util.DBManager;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class StatisticsPresenter extends BasePresenter<IStatisticsView>{

    StatisticsModel mModel;
    protected ExecutorService mExecutor;
    protected Handler mHandler;
    public StatisticsPresenter() {
        mModel = new StatisticsModel();
        mExecutor = Executors.newSingleThreadExecutor();
        mHandler = new Handler();
    }


    public void getStatistics(String user,int operation,String start,String end) {
        System.out.println("getStatistics user = " + user +",operation = " + operation + ",start = " + start + ",end = " + end);
        mIview.showLoading();
        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                List<Rfid> rfids = mModel.queryOperateRfids(user,operation,start,end);
                List<Operation> operations = mModel.queryOperation(user,operation,start,end);
                System.out.println("operations size = " + operations.size() +",rfids size = " +rfids.size());
                List<StatisticsModel.Statistics> list = new ArrayList<>();
                for(Operation operation1 : operations) {
                    StatisticsModel.Statistics statistics = new StatisticsModel.Statistics();
                    for(Rfid rfid : rfids) {
                        System.out.println("rfid.getId() == operation1.getRfid() = " + (rfid.getId() == operation1.getRfid()));
                        if(rfid.getId() == operation1.getRfid()) {
                            statistics.setDate(operation1.getDate());
                            statistics.setRfid(rfid);
                            if(operation == 4) {
                                System.out.println("operation = " + operation + ">" + (list.contains(statistics)));
                                if(!list.contains(statistics))
                                    list.add(statistics);
                            } else {
                                list.add(statistics);
                            }
                        }
                    }
                }
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mIview.stopLoading();
                        mIview.onStaticsRfidGot(list);
                    }
                });
            }
        });
    }

    public void export(List<StatisticsModel.Statistics> statistics) {
        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                mModel.export(statistics, new StatisticsModel.ExportListener() {
                    @Override
                    public void onExport(float rate, StatisticsModel.Statistics statistics) {
                        System.out.println("rate = " + rate * 100);
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                mIview.onExport(rate,statistics);
                            }
                        });

                    }
                });
            }
        });
    }

}
